package com.symbio.ishenapi.order.tencent;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;

import com.alibaba.dubbo.config.annotation.Service;
import com.symbio.ishenapi.order.tencent.common.Configure;
import com.symbio.ishenapi.order.tencent.common.HttpsRequest;
import com.symbio.ishenapi.order.tencent.common.Util;
import com.symbio.ishenapi.order.tencent.common.report.protocol.UniCallbackResData;
import com.symbio.ishenapi.order.tencent.protocol.pay_protocol.AppPayReqData;
import com.symbio.ishenapi.order.tencent.protocol.refund_protocol.RefundReqData;
import com.symbio.ishenapi.order.tencent.protocol.refund_protocol.RefundResData;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;


/**
 *
 * @author victor Date 2015年10月15日 上午9:26:41
 */

public class WxpayServiceImpl implements WxpayService {
	protected static final Log logger = LogFactory.getLog(WxpayServiceImpl.class);

	@Override
	public String formatData(Object postData) {

		XStream xStreamForRequestPostData = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));

		return xStreamForRequestPostData.toXML(postData);

	}

	@Override
	public  UniCallbackResData placeOrder(AppPayReqData postdata) {

		String url = Configure.getUNIFIED_ORDER_API();

		UniCallbackResData rsModel = null;

		try {

			HttpsRequest hsq = null;
			try {
				hsq = new HttpsRequest();
			} catch (Exception e) {
				logger.info("你好：" + e.getMessage());
				e.printStackTrace();
			}

			String result = hsq.sendPost(url, postdata);

			rsModel = (UniCallbackResData) Util.getObjectFromXML(result, UniCallbackResData.class);

		} catch (UnrecoverableKeyException | KeyManagementException | NoSuchAlgorithmException | KeyStoreException
				| IOException e) {
			logger.info("微信支付下单出错！：" + e.getMessage());
			return null;
		} catch (Exception e) {
			logger.info("微信支付下单出错：" + e.getMessage());
			return null;
		}

		return rsModel;

	}
	
	@Override
	public RefundResData RefundOrder(RefundReqData postdata) {
		
		String url = Configure. getREFUND_API();

		RefundResData rsModel = null;

		try {

			HttpsRequest hsq = null;
			try {
				hsq = new HttpsRequest();
			} catch (Exception e) {
				logger.info("你好：" + e.getMessage());
				e.printStackTrace();
			}

			String result = hsq.sendPost(url, postdata);

			rsModel = (RefundResData) Util.getObjectFromXML(result, RefundResData.class);
			
		} catch (UnrecoverableKeyException | KeyManagementException | NoSuchAlgorithmException | KeyStoreException
				| IOException e) {
			logger.info("微信支付退单出错！：" + e.getMessage());
			return null;
		} catch (Exception e) {
			logger.info("微信支付退单出错：" + e.getMessage());
			return null;
		}

		return rsModel;
	}

}
